<template>
  <div class="app-container">
    <!-- 页面标题区域 -->
    <div class="page-header">
      <div class="header-content">
        <div class="title-section">
          <i class="el-icon-goods header-icon"></i>
          <h2 class="page-title">商品信息管理</h2>
          <span class="page-subtitle">管理商品信息、库存、价格等数据</span>
        </div>
        <div class="header-actions">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="medium"
            @click="handleAdd"
            v-hasPermi="['info:info:add']"
            class="add-btn"
          >
            新增商品
          </el-button>
        </div>
      </div>
    </div>

    <!-- 搜索表单区域 -->
    <el-card class="search-card" shadow="hover">
      <div slot="header" class="search-header">
        <i class="el-icon-search"></i>
        <span>搜索条件</span>
        <el-button 
          type="text" 
          @click="toggleSearch"
          class="toggle-btn"
        >
          {{ showSearch ? '收起' : '展开' }}
          <i :class="showSearch ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
        </el-button>
      </div>
      
      <el-form 
        :model="queryParams" 
        ref="queryForm" 
        size="small" 
        :inline="true" 
        v-show="showSearch" 
        label-width="80px"
        class="search-form"
      >
        <el-row :gutter="20">
          <el-col :span="6">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
                prefix-icon="el-icon-goods"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
          </el-col>
          <el-col :span="6">
      <el-form-item label="库存" prop="stock">
        <el-input
          v-model="queryParams.stock"
                placeholder="请输入库存数量"
          clearable
                prefix-icon="el-icon-box"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
          </el-col>
          <el-col :span="6">
      <el-form-item label="销量" prop="salesVolume">
        <el-input
          v-model="queryParams.salesVolume"
          placeholder="请输入销量"
          clearable
                prefix-icon="el-icon-sell"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
                <el-option label="上架" value="1" />
                <el-option label="下架" value="0" />
              </el-select>
      </el-form-item>
          </el-col>
        </el-row>
        
        <div class="search-actions">
          <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery" class="search-btn">
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="medium" @click="resetQuery" class="reset-btn">
            重置
          </el-button>
        </div>
    </el-form>
    </el-card>

    <!-- 操作按钮区域 -->
    <el-card class="action-card" shadow="hover">
      <div class="action-buttons">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="medium"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['info:info:edit']"
          class="action-btn edit-btn"
        >
          修改商品
        </el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="medium"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['info:info:remove']"
          class="action-btn delete-btn"
        >
          删除商品
        </el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="medium"
          @click="handleExport"
          v-hasPermi="['info:info:export']"
          class="action-btn export-btn"
        >
          导出数据
        </el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" class="toolbar"></right-toolbar>
      </div>
    </el-card>

    <!-- 数据表格区域 -->
    <el-card class="table-card" shadow="hover">
      <div slot="header" class="table-header">
        <i class="el-icon-document"></i>
        <span>商品列表</span>
        <span class="total-count">共 {{ total }} 条记录</span>
      </div>
      
      <el-table 
        v-loading="loading" 
        :data="infoList" 
        @selection-change="handleSelectionChange"
        class="data-table"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        stripe
        highlight-current-row
      >
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="商品ID" align="center" prop="id" width="80" />
        <el-table-column label="分类ID" align="center" prop="parentId" width="80" />
        <el-table-column label="商品名称" align="left" prop="goodsName" min-width="150">
          <template slot-scope="scope">
            <div class="goods-name">
              <i class="el-icon-goods goods-icon"></i>
              <span>{{ scope.row.goodsName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商品描述" align="left" prop="goodsDesc" min-width="200" show-overflow-tooltip />
        <el-table-column label="价格信息" align="center" width="150">
          <template slot-scope="scope">
            <div class="price-info">
              <div class="current-price">¥{{ scope.row.currentPrice }}</div>
              <div class="original-price">¥{{ scope.row.originalPrice }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="库存" align="center" prop="stock" width="100">
          <template slot-scope="scope">
            <el-tag 
              :type="scope.row.stock < 10 ? 'danger' : scope.row.stock < 50 ? 'warning' : 'success'"
              size="small"
            >
              {{ scope.row.stock }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="销量" align="center" prop="salesVolume" width="100">
          <template slot-scope="scope">
            <div class="sales-volume">
              <i class="el-icon-sell"></i>
              <span>{{ scope.row.salesVolume }}</span>
            </div>
          </template>
        </el-table-column>
      <el-table-column label="封面图" align="center" prop="coverImg" width="100">
        <template slot-scope="scope">
            <div class="image-container">
              <image-preview :src="scope.row.coverImg" :width="60" :height="60"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="详情图" align="center" prop="detailImgs" width="100">
          <template slot-scope="scope">
            <div class="image-container">
              <image-preview :src="scope.row.detailImgs" :width="60" :height="60"/>
            </div>
        </template>
      </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
            <el-tag 
              :type="scope.row.status == 1 ? 'success' : 'info'"
              size="small"
            >
              {{ scope.row.status == 1 ? '上架' : '下架' }}
            </el-tag>
        </template>
      </el-table-column>
        <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
              type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['info:info:edit']"
              class="table-btn edit-btn"
            >
              编辑
            </el-button>
          <el-button
            size="mini"
              type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['info:info:remove']"
              class="table-btn delete-btn"
            >
              删除
            </el-button>
        </template>
      </el-table-column>
    </el-table>

      <!-- 分页组件 -->
      <div class="pagination-container">
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
      </div>
    </el-card>

    <!-- 添加或修改商品信息对话框 -->
    <el-dialog 
      :title="title" 
      :visible.sync="open" 
      width="700px" 
      append-to-body
      class="goods-dialog"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="goods-form">
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="商品名称" prop="goodsName">
              <el-input 
                v-model="form.goodsName" 
                placeholder="请输入商品名称"
                prefix-icon="el-icon-goods"
              />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类ID" prop="parentId">
              <el-input 
                v-model="form.parentId"  
                placeholder="请输入分类ID"
                prefix-icon="el-icon-folder"
              />
        </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="商品描述" prop="goodsDesc">
          <el-input 
            v-model="form.goodsDesc" 
            type="textarea" 
            :rows="3"
            placeholder="请输入商品详细描述"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
        
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="原价" prop="originalPrice">
              <el-input 
                v-model="form.originalPrice" 
                placeholder="请输入原价"
                prefix-icon="el-icon-money"
              >
                <template slot="prepend">¥</template>
              </el-input>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="现价" prop="currentPrice">
              <el-input 
                v-model="form.currentPrice" 
                placeholder="请输入现价"
                prefix-icon="el-icon-money"
              >
                <template slot="prepend">¥</template>
              </el-input>
        </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="库存" prop="stock">
              <el-input-number 
                v-model="form.stock" 
                placeholder="请输入库存"
                :min="0"
                :max="99999"
                style="width: 100%"
              />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="销量" prop="salesVolume">
              <el-input-number 
                v-model="form.salesVolume" 
                placeholder="请输入销量"
                :min="0"
                :max="99999"
                style="width: 100%"
              />
        </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="封面图" prop="coverImg">
          <image-upload v-model="form.coverImg"/>
          <div class="upload-tip">建议尺寸：800x800px，支持JPG、PNG格式</div>
        </el-form-item>
        
        <el-form-item label="详情图列表" prop="detailImgs">
          <image-upload v-model="form.detailImgs"/>
          <div class="upload-tip">建议尺寸：1200x800px，支持JPG、PNG格式</div>
        </el-form-item>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" class="cancel-btn">取 消</el-button>
        <el-button type="primary" @click="submitForm" class="submit-btn">确 定</el-button>
      </div>
    </el-dialog>

      <!-- 库存不足弹窗 -->
      <el-dialog
        title="库存预警"
        :visible.sync="showDialog"
      width="500px"
      append-to-body
      class="stock-dialog"
    >
      <div class="stock-warning">
        <i class="el-icon-warning warning-icon"></i>
        <div class="warning-content">
          <h4>库存不足提醒</h4>
          <p>以下商品库存不足，请及时补货：</p>
        </div>
      </div>
      
      <div v-if="lowStockList && lowStockList.length > 0" class="low-stock-list">
        <el-table :data="lowStockList" size="small" class="stock-table">
          <el-table-column label="商品名称" prop="goodsName" />
          <el-table-column label="当前库存" prop="stock" width="100">
            <template slot-scope="scope">
              <el-tag type="danger" size="mini">{{ scope.row.stock }}</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false" type="primary">知道了</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo ,getLowStock} from "@/api/info/info"

export default {
  name: "Info",
  data() {
    return {
      showDialog: false,
      lowStockList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        goodsName: null,
        stock: null,
        salesVolume: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "分类ID不能为空", trigger: "blur" }
        ],
        goodsName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        originalPrice: [
          { required: true, message: "原价不能为空", trigger: "blur" }
        ],
        currentPrice: [
          { required: true, message: "现价不能为空", trigger: "blur" }
        ],
        stock: [
          { required: true, message: "库存不能为空", trigger: "blur" }
        ],
        salesVolume: [
          { required: true, message: "销量不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态(0下架,1上架)不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
    // 页面加载时检查一次
    this.checkStock();
    // 每5分钟检查一次
    setInterval(() => this.checkStock(), 5 * 60 * 1000);
  },
  methods: {
    // 切换搜索区域显示/隐藏
    toggleSearch() {
      this.showSearch = !this.showSearch;
    },
    
    checkStock() {
      getLowStock().then(res => {
        // 有库存不足商品且弹窗未显示，则显示弹窗
        if (res.data && res.data.length > 0 && !this.showDialog) {
          this.lowStockList = res.data;
          this.showDialog = true;
        }
      })
    },
    /** 查询商品信息列表 */
    getList() {
      this.loading = true
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        goodsName: null,
        goodsDesc: null,
        originalPrice: null,
        currentPrice: null,
        stock: null,
        salesVolume: null,
        coverImg: null,
        detailImgs: null,
        status: null,
        createTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加商品信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改商品信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除商品信息编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('info/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped>
/* 页面整体样式 */
.app-container {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 84px);
}

/* 页面标题区域 */
.page-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  padding: 30px;
  margin-bottom: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title-section {
  color: white;
}

.header-icon {
  font-size: 32px;
  margin-right: 15px;
  vertical-align: middle;
}

.page-title {
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 8px 0;
  display: inline-block;
  vertical-align: middle;
}

.page-subtitle {
  font-size: 16px;
  opacity: 0.9;
  display: block;
}

.add-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 2px solid rgba(255, 255, 255, 0.3);
  color: white;
  font-weight: 600;
  padding: 12px 24px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.add-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
}

/* 搜索卡片 */
.search-card {
  margin-bottom: 20px;
  border-radius: 12px;
  border: none;
}

.search-header {
  display: flex;
  align-items: center;
  font-weight: 600;
  color: #303133;
}

.search-header i {
  margin-right: 8px;
  color: #409eff;
}

.toggle-btn {
  margin-left: auto;
  color: #409eff;
}

.search-form {
  padding: 20px 0 0 0;
}

.search-actions {
  text-align: center;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #ebeef5;
}

.search-btn, .reset-btn {
  padding: 10px 24px;
  border-radius: 8px;
  font-weight: 500;
}

.search-btn {
  background: linear-gradient(135deg, #409eff 0%, #36a3f7 100%);
  border: none;
}

.reset-btn {
  background: #f5f7fa;
  border: 1px solid #dcdfe6;
  color: #606266;
}

/* 操作按钮卡片 */
.action-card {
  margin-bottom: 20px;
  border-radius: 12px;
  border: none;
}

.action-buttons {
  display: flex;
  gap: 15px;
  align-items: center;
}

.action-btn {
  padding: 10px 20px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.edit-btn {
  background: linear-gradient(135deg, #67c23a 0%, #85ce61 100%);
  border: none;
  color: white;
}

.delete-btn {
  background: linear-gradient(135deg, #f56c6c 0%, #f78989 100%);
  border: none;
  color: white;
}

.export-btn {
  background: linear-gradient(135deg, #e6a23c 0%, #ebb563 100%);
  border: none;
  color: white;
}

/* 表格卡片 */
.table-card {
  border-radius: 12px;
  border: none;
}

.table-header {
  display: flex;
  align-items: center;
  font-weight: 600;
  color: #303133;
}

.table-header i {
  margin-right: 8px;
  color: #409eff;
}

.total-count {
  margin-left: auto;
  color: #909399;
  font-size: 14px;
}

.data-table {
  margin: 20px 0;
}

/* 表格内容样式 */
.goods-name {
  display: flex;
  align-items: center;
  font-weight: 500;
}

.goods-icon {
  color: #409eff;
  margin-right: 8px;
}

.price-info {
  text-align: center;
}

.current-price {
  color: #f56c6c;
  font-size: 16px;
  font-weight: 600;
}

.original-price {
  color: #909399;
  font-size: 12px;
  text-decoration: line-through;
  margin-top: 4px;
}

.sales-volume {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #67c23a;
}

.sales-volume i {
  margin-right: 6px;
}

.image-container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.table-btn {
  margin: 2px;
  border-radius: 6px;
}

.edit-btn {
  background: #67c23a;
  border-color: #67c23a;
}

.delete-btn {
  background: #f56c6c;
  border-color: #f56c6c;
}

/* 分页容器 */
.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

/* 弹窗样式 */
.goods-dialog .el-dialog__header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 8px 8px 0 0;
}

.goods-dialog .el-dialog__title {
  color: white;
  font-weight: 600;
}

.goods-form {
  padding: 20px 0;
}

.upload-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 8px;
  line-height: 1.4;
}

.dialog-footer {
  text-align: right;
  padding-top: 20px;
  border-top: 1px solid #ebeef5;
}

.cancel-btn {
  background: #f5f7fa;
  border: 1px solid #dcdfe6;
  color: #606266;
  margin-right: 15px;
}

.submit-btn {
  background: linear-gradient(135deg, #409eff 0%, #36a3f7 100%);
  border: none;
  padding: 10px 24px;
}

/* 库存预警弹窗 */
.stock-dialog .el-dialog__header {
  background: linear-gradient(135deg, #f56c6c 0%, #f78989 100%);
  color: white;
  border-radius: 8px 8px 0 0;
}

.stock-warning {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding: 20px;
  background: #fef0f0;
  border-radius: 8px;
  border-left: 4px solid #f56c6c;
}

.warning-icon {
  font-size: 24px;
  color: #f56c6c;
  margin-right: 15px;
}

.warning-content h4 {
  margin: 0 0 8px 0;
  color: #f56c6c;
  font-size: 16px;
}

.warning-content p {
  margin: 0;
  color: #606266;
}

.low-stock-list {
  margin-top: 20px;
}

.stock-table {
  border: 1px solid #ebeef5;
  border-radius: 6px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .header-content {
    flex-direction: column;
    text-align: center;
  }
  
  .header-actions {
    margin-top: 20px;
  }
  
  .action-buttons {
    flex-wrap: wrap;
    justify-content: center;
  }
}

@media (max-width: 768px) {
  .app-container {
    padding: 15px;
  }
  
  .page-header {
    padding: 20px;
  }
  
  .page-title {
    font-size: 24px;
  }
  
  .search-form .el-col {
    margin-bottom: 15px;
  }
  
  .action-buttons {
    gap: 10px;
  }
  
  .action-btn {
    padding: 8px 16px;
    font-size: 12px;
  }
}

/* 动画效果 */
.el-card {
  transition: all 0.3s ease;
}

.el-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.el-button {
  transition: all 0.3s ease;
}

.el-table {
  border-radius: 8px;
  overflow: hidden;
}

.el-table th {
  background: #f5f7fa !important;
  color: #606266 !important;
  font-weight: 600;
}

.el-table--striped .el-table__body tr.el-table__row--striped td {
  background: #fafafa;
}

.el-table--striped .el-table__body tr:hover > td {
  background: #f0f9ff !important;
}
</style>
